package com.syp.hehe.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.syp.hehe.dao.ArticleMapper;
import com.syp.hehe.domain.ArticleTypeEnum;
import com.syp.hehe.domain.RecordStatusEnum;
import com.syp.hehe.domain.pojo.ArticleSchema;
import com.syp.hehe.domain.vo.ArticleVo;
import com.syp.hehe.service.ArticleServiceI;
import com.syp.hehe.util.PicDiskAdapter;
import com.syp.hehe.util.paging.Page;

@Service("articleService")
public class ArticleServiceImpl implements ArticleServiceI {

	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public ArticleVo getById(Long id) {
		ArticleSchema articleSchema = this.articleMapper.getById(id);
		ArticleVo article = new ArticleVo();
		article.setId(articleSchema.getId());
		article.setContent(articleSchema.getContent());
		article.setPic(null);
		article.setType(articleSchema.getType());
		article.setPublishtime(articleSchema.getPublishtime());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		article.setPublishtimeText(df.format(articleSchema.getPublishtime()));
		article.setAuthorUsername(articleSchema.getAuthorUsername());
		return article;
	}

	@Override
	public void insert(ArticleVo article) {
		ArticleSchema articleSchema = new ArticleSchema();
		
		articleSchema.setUuid(article.getUuid());
		articleSchema.setContent(article.getContent().replace("\r\n", "<br/>").replace("\n", "<br/>"));
		if (article.getPic().getSize() == 0L) {
			articleSchema.setType(ArticleTypeEnum.DUAN.getCode());
		} else {
			articleSchema.setType(ArticleTypeEnum.PIC.getCode());

			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			String strDateSubfix = df.format(Calendar.getInstance().getTime());

			StringBuilder sb = new StringBuilder();
			sb.append("/").append(strDateSubfix);
			sb.append("/").append(UUID.randomUUID().toString());
			sb.append(".").append(article.getPic().getContentType().split("/")[1]);
			articleSchema.setPicPath(sb.toString());
			PicDiskAdapter.ADAPTER.write(article.getPic(), articleSchema.getPicPath());
		}
		articleSchema.setAuthorUsername(article.getAuthorUsername());
		articleSchema.setPublishtime(Calendar.getInstance().getTime());
		articleSchema.setStatus(RecordStatusEnum.NORMAL.getStatus());

		this.articleMapper.insert(articleSchema);
	}

	@Override
	public Page<ArticleVo> getPageList(Integer type, Long pageNum, Integer pageSize) {
		Page<ArticleVo> page = new Page<ArticleVo>();
		List<ArticleVo> list = new ArrayList<ArticleVo>();
		Long beginIndex = (pageNum - 1) * pageSize;
		List<ArticleSchema> articleSchemaList = this.articleMapper.getPageList(type, beginIndex, pageSize);
		for (ArticleSchema item : articleSchemaList) {
			ArticleVo article = new ArticleVo();

			article.setId(item.getId());
			article.setContent(item.getContent());
			article.setPicPath(item.getPicPath());
			article.setType(item.getType());
			article.setPublishtime(item.getPublishtime());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			article.setPublishtimeText(df.format(item.getPublishtime()));
			article.setAuthorUsername(item.getAuthorUsername());

			list.add(article);
		}
		page.setPageIndex(pageNum);
		page.setPageSize(pageSize);
		page.setDatas(list);
		
		Boolean hasNextPage = false;
		if(page.getDatas().size() < pageSize){
			hasNextPage = false;
		} else if(page.getDatas().size() == pageSize){
			hasNextPage = this.articleMapper.hasMoreRow(type, beginIndex + pageSize);
		}
		page.setHasNextPage(hasNextPage);
		return page;
	}

	@Override
	public byte[] getPicBytes(Long id) {
		byte[] bytes = null;
		ArticleSchema articleSchema = this.articleMapper.getById(id);
		if (articleSchema != null) {
			bytes = PicDiskAdapter.ADAPTER.read(articleSchema.getPicPath());
		}
		return bytes;

	}

}
